package com.bdqn.controller;

import com.bdqn.pojo.SmbmsUser;
import com.bdqn.serice.SmbmsUserService;
import com.bdqn.utils.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author www
 * @Description: 实现登录控制器
 * @date 2020-05-30 16:47
 */
@Controller
public class LoginController {
    @Resource
    private SmbmsUserService smbmsUserService;

    /**
     * 登录
     * @param userCode
     * @param userPassword
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public String login(String userCode,String userPassword, HttpServletRequest request, Model model)throws Exception {
        System.out.println(userCode+"/"+userPassword);
        SmbmsUser user = smbmsUserService.login(userCode, userPassword);

        if (user != null) {
            System.out.println("登陆成功!");
            request.getSession().setAttribute(Constants.USER_SESSION, user);
            return "frame";
        }else {
            model.addAttribute("error", "用户名或密码不正确");
            return "login";
        }
    }

    /**
     * 注销
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout.do")
    public String logout(HttpSession session) {
        //清空用户信息
        session.removeAttribute(Constants.USER_SESSION);
        return "login";
    }
}
